package com.brkj.course;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import com.brkj.httpInterface.BaseAjaxParams;

import android.content.Context;

import com.brkj.model.DS_CourseStudyTimes;
import com.brkj.util.ConstAnts;
import com.brkj.util.JsonUtil;
import com.brkj.util.MyApplication;
import com.brkj.httpInterface.HttpInterface.HIF_RecodeCourseStudyTimes;
import com.google.gson.Gson;

public class RecodeStudyTimes {

	private FinalDb db;
	private Context context;
	
	public RecodeStudyTimes(Context context){
		this.context = context;
		db = FinalDb.create(context, ConstAnts.BRKJ_DB);
	}
	
	public  void recodeOne(final String courseID){
		DS_CourseStudyTimes studyTimes = new DS_CourseStudyTimes();
		studyTimes.setCourseID(courseID);
		studyTimes.setTimes("1");
		
		List<DS_CourseStudyTimes> studyTimesList = new ArrayList<DS_CourseStudyTimes>();
		studyTimesList.add(studyTimes);
		Gson gson = new Gson();
		String jsonStr = gson.toJson(studyTimesList);
		
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_RecodeCourseStudyTimes.params.UserID, MyApplication.myUserID);
		params.put(HIF_RecodeCourseStudyTimes.params.CourseStudyTimes, jsonStr);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_RecodeCourseStudyTimes.address, params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, strMsg);
				saveStudyTimes(courseID);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				HIF_RecodeCourseStudyTimes.result result = 
						JsonUtil.fromJson((String)t, HIF_RecodeCourseStudyTimes.result.class);
				
				if(result == null){
					saveStudyTimes(courseID);
					return;
				}else{
					if(result.result == 1){
						return;
					}else{
						saveStudyTimes(courseID);
					}
				} 
			}
			
			
		});
	}
	
	public void uploadLocalData(){
		final List<DS_CourseStudyTimes> studyTimesList = 
				db.findAll(DS_CourseStudyTimes.class);
		
		Gson gson = new Gson();
		String jsonStr = gson.toJson(studyTimesList);
		
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_RecodeCourseStudyTimes.params.UserID, MyApplication.myUserID);
		params.put(HIF_RecodeCourseStudyTimes.params.CourseStudyTimes, jsonStr);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_RecodeCourseStudyTimes.address, params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, strMsg);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				HIF_RecodeCourseStudyTimes.result result = 
						JsonUtil.fromJson((String)t, HIF_RecodeCourseStudyTimes.result.class);
				
				if(result== null){
					return;
				}
				//上传成功，置0
				if(result.result == 1){
					for(DS_CourseStudyTimes studyTime : studyTimesList){
						
						db.deleteByWhere(DS_CourseStudyTimes.class, " CourseID='"+studyTime.getCourseID()+ "'");
					}
				}
			}
			
			
		});
	}
	
	private void saveStudyTimes(String courseID){
		DS_CourseStudyTimes times = new DS_CourseStudyTimes();
		times.setCourseID(courseID);
		
		List<DS_CourseStudyTimes> studyTimesList = 
				db.findAllByWhere(DS_CourseStudyTimes.class, " CourseID='" + courseID + "'");
		if((studyTimesList!=null) && (studyTimesList.size()>0)){
			
			times.setTimes(Integer.toString(1 + Integer.valueOf(studyTimesList.get(0).getTimes())));
			db.update(times, " CourseID='" + courseID+ "'");
		}else{
			times.setTimes("1");
			db.save(times);
		}
		
	}
}
